package cn.lu.train.util;

import android.support.v4.app.Fragment;
import android.util.SparseArray;

import cn.lu.train.base.BaseFragment;

public class FragmentMG {

    private SparseArray<Fragment> mSparseArray;

    private FragmentMG(){
        mSparseArray = new SparseArray<>();
    }

    public static FragmentMG get() {
        return Holder.instance;
    }

    public void add(int key, Fragment fragment) {
        mSparseArray.put(key, fragment);
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseFragment> T getFragment(int key) {
        Fragment f = mSparseArray.get(key);
        if (f != null) {
            return (T) f;
        }
        return null;
    }

    public void removeFragment(int key) {
        mSparseArray.delete(key);
    }

    private static class Holder{
        private static FragmentMG instance = new FragmentMG();
    }
}
